<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="listQuery.name" placeholder="菜单名称" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        查询
      </el-button>
      <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-edit" @click="handleCreate">
        新增
      </el-button>
      <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-edit" @click="handleEnable">
        批量启用
      </el-button>
      <el-button class="filter-item" style="margin-left: 10px;" type="danger" icon="el-icon-edit" @click="handleDisable">
        批量禁用
      </el-button>
    </div>

    <el-table
      :data="tableData"
      style="width: 100%;margin-bottom: 20px;"
      row-key="id"
      border
      default-expand-all
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
      @selection-change="tableSelected"
    >
      <el-table-column
        type="selection"
        width="55"
      />
      <el-table-column
        prop="name"
        label="部门名称"
        sortable
        width="380"
      />
      <el-table-column
        prop="seq"
        width="100"
        label="显示顺序"
      />
      <el-table-column
        prop="code"
        width="580"
        label="编码"
      />
      <el-table-column
        prop="status"
        width="120"
        label="状态"
      >
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.status===1"
            size="mini"
            @click="handleEditStatus(scope.$index, scope.row,0)"
          >启用</el-button>
          <el-button
            v-if="scope.row.status===0"
            size="mini"
            type="danger"
            @click="handleEditStatus(scope.$index, scope.row,1)"
          >禁用</el-button>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="handleEdit(scope.$index, scope.row)"
          >编辑</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" width="300">
      <el-form ref="dataForm" :rules="rules" :model="temp" label-position="left" label-width="70px" style="width: 400px; margin-left:50px;">
        <el-form-item label="上级" prop="pid">
          <el-tree
            ref="tree"
            v-model="temp.pid"
            :data="treeData"
            show-checkbox
            node-key="id"
            check-strictly="true"
            :props="menuTreeProps"
            @check-change="treeCheck"
          />
        </el-form-item>
        <el-form-item label="名称" prop="name">
          <el-input v-model="temp.name" />
        </el-form-item>
        <el-form-item label="显示顺序" prop="seq">
          <el-input v-model="temp.seq" size="mini" />
        </el-form-item>
        <el-form-item label="code" prop="code">
          <el-input v-model="temp.code" size="mini" />
        </el-form-item>
        <el-form-item label="状态">
          <el-switch
            v-model="temp.status"
            active-text="启用"
            inactive-text="禁用"
            active-value="0"
            inactive-value="1"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">
          取消
        </el-button>
        <el-button type="primary" @click="dialogStatus==='create'?createData():updateData()">
          确认
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { tree, update, create, setStatus } from '@/api/depart'

export default {
  data() {
    return {
      tableData: [],
      listQuery: {},
      tableSelectedKeys: [],
      dialogFormVisible: false,
      dialogStatus: '',
      treeData: [],
      menuTreeProps: { label: 'name' },
      textMap: {
        update: '编辑',
        create: '新建'
      },
      temp: {
        id: undefined,
        name: '',
        code: '',
        status: 0,
        seq: 0,
        pid: 0
      },
      tableSelectedKeys: [],
      rules: {
        name: [{ required: true, message: 'name is required', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      tree(this.listQuery).then(response => {
        this.tableData = response.data.data
        this.treeData = this.tableData

        // Just to simulate the time of the request
        setTimeout(() => {
          this.listLoading = false
        }, 1.5 * 1000)
      })
    },
    createData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          const tempData = Object.assign({}, this.temp)
          create(tempData).then(res => {
            const response = res.data
            if (response.success) {
              this.getList()
              this.dialogFormVisible = false
            } else {
              this.$message({
                message: response.msg,
                type: 'failed'
              })
            }
          })
        }
      })
    },
    updateData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          const tempData = Object.assign({}, this.temp)
          update(tempData).then(res => {
            const response = res.data
            if (response.success) {
              this.getList()
              this.dialogFormVisible = false
            } else {
              this.$message({
                message: response.msg,
                type: 'failed'
              })
            }
          })
        }
      })
    },
    handleFilter() {
      this.getList()
    },
    handleCreate() {
      this.resetTemp()
      this.dialogFormVisible = true
      this.dialogStatus = 'create'
      this.$refs.tree.setCheckedKeys([1])
    },
    resetTemp() {
      this.temp = {
        id: undefined,
        name: '',
        code: '',
        status: 0,
        seq: 0,
        pid: 0
      }
    },
    handleEdit(index, row) {
      this.dialogFormVisible = true
      this.dialogStatus = 'update'
      this.temp = Object.assign({}, row)
      this.$refs.tree.setCheckedKeys([row.pid])
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    handleEditStatus(index, row) {

    },
    handleEnable() {

    },
    handleDisable() {

    },
    tableSelected(selectedData) {
      this.tableSelectedKeys = []
      selectedData.forEach(a => {
        this.tableSelectedKeys.push(a.id)
      })
    },
    treeCheck(data, checked, node) {
      if (checked) {
        this.$refs.tree.setCheckedNodes([data])
        this.temp.pid = this.$refs.tree.getCheckedKeys()[0]
      }
    }
  }
}
</script>
